/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.devicetime.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.devicetime.enums.DeviceTimeStatusEnum;
import vip.xiaonuo.biz.modular.devicetype.param.DeviceTypeIdParam;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.devicetime.entity.DeviceTime;
import vip.xiaonuo.biz.modular.devicetime.mapper.DeviceTimeMapper;
import vip.xiaonuo.biz.modular.devicetime.param.DeviceTimeAddParam;
import vip.xiaonuo.biz.modular.devicetime.param.DeviceTimeEditParam;
import vip.xiaonuo.biz.modular.devicetime.param.DeviceTimeIdParam;
import vip.xiaonuo.biz.modular.devicetime.param.DeviceTimePageParam;
import vip.xiaonuo.biz.modular.devicetime.service.DeviceTimeService;

import java.util.List;

/**
 * 设备类型时间段Service接口实现类
 *
 * @author Blurryface
 * @date  2025/07/06 15:17
 **/
@Service
public class DeviceTimeServiceImpl extends ServiceImpl<DeviceTimeMapper, DeviceTime> implements DeviceTimeService {

    @Override
    public Page<DeviceTime> page(DeviceTimePageParam deviceTimePageParam) {
        QueryWrapper<DeviceTime> queryWrapper = new QueryWrapper<DeviceTime>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(deviceTimePageParam.getName())) {
            queryWrapper.lambda().like(DeviceTime::getName, deviceTimePageParam.getName());
        }
        if(ObjectUtil.isNotEmpty(deviceTimePageParam.getOrgId())) {
            queryWrapper.lambda().eq(DeviceTime::getOrgId, deviceTimePageParam.getOrgId());
        }
        if(ObjectUtil.isNotEmpty(deviceTimePageParam.getDevicetypeId())) {
            queryWrapper.lambda().eq(DeviceTime::getDevicetypeId, deviceTimePageParam.getDevicetypeId());
        }
        if(ObjectUtil.isNotEmpty(deviceTimePageParam.getDisable())) {
            queryWrapper.lambda().eq(DeviceTime::getDisable, deviceTimePageParam.getDisable());
        }
        if(ObjectUtil.isAllNotEmpty(deviceTimePageParam.getSortField(), deviceTimePageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(deviceTimePageParam.getSortOrder());
            queryWrapper.orderBy(true, deviceTimePageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(deviceTimePageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(DeviceTime::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(DeviceTimeAddParam deviceTimeAddParam) {
        DeviceTime deviceTime = BeanUtil.toBean(deviceTimeAddParam, DeviceTime.class);
        deviceTime.setName(deviceTime.getName().replace('：',':').replace(" ",""));
        this.save(deviceTime);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(DeviceTimeEditParam deviceTimeEditParam) {
        DeviceTime deviceTime = this.queryEntity(deviceTimeEditParam.getId());
        BeanUtil.copyProperties(deviceTimeEditParam, deviceTime);
        this.updateById(deviceTime);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<DeviceTimeIdParam> deviceTimeIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(deviceTimeIdParamList, DeviceTimeIdParam::getId));
    }

    @Override
    public DeviceTime detail(DeviceTimeIdParam deviceTimeIdParam) {
        return this.queryEntity(deviceTimeIdParam.getId());
    }

    @Override
    public DeviceTime queryEntity(String id) {
        DeviceTime deviceTime = this.getById(id);
        if(ObjectUtil.isEmpty(deviceTime)) {
            throw new CommonException("设备类型时间段不存在，id值为：{}", id);
        }
        return deviceTime;
    }

    @Override
    public List<String> getDeviceTimeListByTypeId(DeviceTypeIdParam deviceTypeIdParam) {
        QueryWrapper<DeviceTime> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(DeviceTime::getName)
                .eq(DeviceTime::getDevicetypeId, deviceTypeIdParam.getId())
                .eq(DeviceTime::getDisable, DeviceTimeStatusEnum.ENABLE.getValue());

        return this.list(queryWrapper).stream().map(DeviceTime::getName).toList();
    }
}
